﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CodeDynamiksEngine;

using MESCodeFile;

namespace mes_tublay
{
    public partial class frmBusinessPermit : Form
    {
        CodeScript script;
        CodeProcessor proc = new CodeProcessor(ThreadingMode.Normal);
        DataCache dat = new DataCache("myData");
        
        public frmBusinessPermit()
        {
            InitializeComponent();
            InitializeFrameSliderForm();
        }

        private void InitializeForm()
        {
            btnNew.Click += new EventHandler(btnNew_Click);
            btnSearch.Click += new EventHandler(btnSearch_Click);
            proc.ProcessCompleted += new EventHandler(proc_ProcessCompleted);

            txtSearch.TextChanged += new EventHandler(txtSearch_TextChanged);
            btnRenewal.Click += new EventHandler(btnRenewal_Click);
            btnLocation.Click += new EventHandler(btnLocation_Click);
            btnView.Click += new EventHandler(btnView_Click);
            btnOwnership.Click += new EventHandler(btnOwnership_Click);

            loadDropDownList();
            searchItem();
        }

        void btnSearch_Click(object sender, EventArgs e)
        {
            searchItem();
        }

        void proc_ProcessCompleted(object sender, EventArgs e)
        {
            proc.SafeCall(this, delegate
            {
                if (proc.State == ProcessorFlag.Error ||
                    proc.State == ProcessorFlag.DataRdyErr ||
                    proc.State == ProcessorFlag.ReadyErr)
                {
                    MessageBox.Show(proc.GetProcessorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    proc.ClearErrorFlag();
                    return;
                }
                switch (proc.GetLastProcessName().ToString())
                {
                    case "Search":
                        proc.DataFetch(out dat);
                        //Set the active page before exporting
                        if (dat.GetPageSize() >= 1)
                        {
                            dat.SetActivePage(0);
                            grdBusinessPermit.DataSource = dat.ExportPage();
                         
                        }
                        break;
                }
            }); 
        }
        
        
        //new business permit application
        void btnNew_Click(object sender, EventArgs e)
        {
            Common.PARENT.viewApplicationFormForBusinessPermit();
        }
        //view status of business permit application
        void btnView_Click(object sender, EventArgs e)
        {
            if (grdBusinessPermit.SelectedRows.Count == 1)
            {
                string status = grdBusinessPermit.SelectedRows[0].Cells[4].Value.ToString();
                if ((status.Equals("PENDING") || status.Equals("APPROVED")))
                {
                    myParam info = new myParam();
                    info.action = "VIEW";
                    info.msg = "VIEW BUSINESS PERMIT STATUS.";
                    info.myReferenceNumber = grdBusinessPermit.SelectedRows[0].Cells[0].Value.ToString();
                    info.myBusinessReferenceNumber = grdBusinessPermit.SelectedRows[0].Cells[1].Value.ToString();
                    info.myApplicationNumber = grdBusinessPermit.SelectedRows[0].Cells[2].Value.ToString();
                    info.status = status;
                    Common.PARENT.viewApplicationFormForBusinessPermit(info);
                }
            }
        }
        //renew business permit application
        void btnRenewal_Click(object sender, EventArgs e)
        {
            if (grdBusinessPermit.SelectedRows.Count == 1)
            {
                string status = grdBusinessPermit.SelectedRows[0].Cells[4].Value.ToString();
                if (status.Equals("APPROVED"))
                {
                    myParam info = new myParam();
                    info.action = "RENEWAL";
                    info.msg = "RENEWAL OF BUSINESS PERMIT.";
                    info.myReferenceNumber = grdBusinessPermit.SelectedRows[0].Cells[0].Value.ToString();
                    info.myBusinessReferenceNumber = grdBusinessPermit.SelectedRows[0].Cells[1].Value.ToString();
                    info.myApplicationNumber = grdBusinessPermit.SelectedRows[0].Cells[2].Value.ToString();
                    info.status = status;
                    Common.PARENT.viewApplicationFormForBusinessPermit(info);
                }
            }
        }
        //transfer ownership
        void btnOwnership_Click(object sender, EventArgs e)
        {
            if (grdBusinessPermit.SelectedRows.Count == 1)
            {
                string status = grdBusinessPermit.SelectedRows[0].Cells[4].Value.ToString();
                if ((status.Equals("APPROVED")))
                {
                    myParam info = new myParam();
                    info.action = "TRANSFER OWNERSHIP";
                    info.msg = "TRANSFER OWNERSHIP.";
                    info.myReferenceNumber = grdBusinessPermit.SelectedRows[0].Cells[0].Value.ToString();
                    info.myBusinessReferenceNumber = grdBusinessPermit.SelectedRows[0].Cells[1].Value.ToString();
                    info.myApplicationNumber = grdBusinessPermit.SelectedRows[0].Cells[2].Value.ToString();
                    info.status = status;
                    Common.PARENT.viewApplicationFormForBusinessPermit(info);
                }
            }
        }
        //transfer location
        void btnLocation_Click(object sender, EventArgs e)
        {
            if (grdBusinessPermit.SelectedRows.Count == 1)
            {
                string status = grdBusinessPermit.SelectedRows[0].Cells[4].Value.ToString();
                if (status.Equals("APPROVED"))
                {
                    myParam info = new myParam();
                    info.action = "TRANSFER LOCATION";
                    info.msg = "TRANSFER BUSINESS LOCATION.";
                    info.myReferenceNumber = grdBusinessPermit.SelectedRows[0].Cells[0].Value.ToString();
                    info.myBusinessReferenceNumber = grdBusinessPermit.SelectedRows[0].Cells[1].Value.ToString();
                    info.myApplicationNumber = grdBusinessPermit.SelectedRows[0].Cells[2].Value.ToString();
                    info.status = status;
                    Common.PARENT.viewApplicationFormForBusinessPermit(info);
                }
            }
        }

        //searching
        void txtSearch_TextChanged(object sender, EventArgs e)
        {
            searchItem();
        }
        //method for searching
        void searchItem()
        {
            //Check if the processor is executing script, if it is, return to the calling code.
            if (proc.State != ProcessorFlag.Ready && proc.State != ProcessorFlag.DataRdy) return;

            script = new CodeScript("Search");
            script.Add(CodeIns.SYS_RESET);
            script.Add(CodeIns.DBU_NEW_CORE);
            script.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"), Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            script.Add(CodeIns.DBU_BEGIN_TRANSACTION);
            
            string filter = cmbFilter.Text;
            switch(filter)
            {
                case "ALL":
                    script.Add(CodeIns.DBU_QUERY, "SELECT t1.fReferenceNumber AS 'Reference Number',ffBusinessReferenceNumber AS 'Business Reference Number', ffApplicationNumber AS 'Application Number', ffBusinessName AS 'Business Name', ffStatus AS 'Business Status', fLastName AS 'Last Name', fFirstName AS 'First Name', fMiddleName AS 'Middle Name', fCompanyName AS 'Company Name' FROM tblBusinessOwnerProfile t1," +
                                                    "(SELECT fBusinessReferenceNumber AS ffBusinessReferenceNumber, fApplicationNumber AS ffApplicationNumber, fBusinessName AS ffBusinessName, fReferenceNumber, fBusinessStatus AS ffStatus FROM tblBusinessInformation) AS t2 " +
                                                    " WHERE (t1.fReferenceNumber = t2.fReferenceNumber) AND ((fLastName LIKE '%" + txtSearch.Text + "%') OR (fFirstName LIKE '%" + txtSearch.Text + "%') OR (fMiddleName LIKE '%" + txtSearch.Text + "%')" +
                                                    " OR (ffBusinessName LIKE '%" + txtSearch.Text + "%'))"
                                                    );
                    break;

                default:
                    script.Add(CodeIns.DBU_QUERY, "SELECT t1.fReferenceNumber AS 'Reference Number',ffBusinessReferenceNumber AS 'Business Reference Number', ffApplicationNumber AS 'Application Number', ffBusinessName AS 'Business Name', ffStatus AS 'Business Status', fLastName AS 'Last Name', fFirstName AS 'First Name', fMiddleName AS 'Middle Name', fCompanyName AS 'Company Name' FROM tblBusinessOwnerProfile t1," +
                                                    "(SELECT fBusinessReferenceNumber AS ffBusinessReferenceNumber, fApplicationNumber AS ffApplicationNumber, fBusinessName AS ffBusinessName, fReferenceNumber, fBusinessStatus AS ffStatus FROM tblBusinessInformation WHERE fBusinessStatus = '"+filter+"') AS t2 " +
                                                    " WHERE (t1.fReferenceNumber = t2.fReferenceNumber) AND ((fLastName LIKE '%" + txtSearch.Text + "%') OR (fFirstName LIKE '%" + txtSearch.Text + "%') OR (fMiddleName LIKE '%" + txtSearch.Text + "%')" +
                                                    " OR (ffBusinessName LIKE '%" + txtSearch.Text + "%'))"
                                                    );
                    break;
            }
            script.Add(CodeIns.DBU_END);
            proc.Run(script);
        }
        //generate the collections or items for the dropdownlist
        void loadDropDownList()
        {
            cmbFilter.Items.Add("ALL");
            cmbFilter.Items.Add("APPROVED");
            cmbFilter.Items.Add("PENDING");
            cmbFilter.SelectedIndex = 0;
        }

    }
}
